/*
 * Local.fx
 *
 * Created on 2010-01-18, 20:17:57
 */

package galleryviewer;

import javafx.scene.*;
import javafx.scene.paint.Color;
import javafx.scene.text.*;
import javafx.scene.Group;
import javafx.scene.shape.Shape;
import galleryviewer.data.DataLoader;
import galleryviewer.data.LocalBean;
import javafx.scene.image.*;
import javafx.scene.input.MouseEvent;
import javafx.scene.transform.Transform;
import javafx.scene.layout.*;

import javafx.animation.Timeline;

import javafx.animation.Interpolator;
import javafx.animation.KeyFrame;


/**
 * @author m.deluga, SiMet, jaccus
 */

package var lastLocalId : Integer = 0;
package var allLocals : Local[];

public class Local extends CustomNode{

	override public var onMouseClicked = function(e:MouseEvent) {
		if( Main.currentlyOpenedWindow != null)
	       Main.currentlyOpenedWindow.close();
	    Main.currentlyOpenedWindow = showDetails(Main.stage.scene,e);
	    println("local {localId} clicked!");
	}

	public var localId: Integer;
	
	public var floor: GalleryFloor;
	
	var localBean : LocalBean = bind DataLoader.getLocal(localId);
	
	public var localImage: Image = Image { url: "{__DIR__}img/locals/{localBean.getImageurl()}" backgroundLoading: true};		
	
	var plainLocalColor = Color.web("#e6e6e6");
	var filteredLocalColor = bind Controllers.currentHighlightColor;
	var localColor : Color = bind if(localBean.keywordsContain(Controllers.selectedFilter)) filteredLocalColor else plainLocalColor;
	var hoverColor = bind if(localBean.keywordsContain(Controllers.selectedFilter)) Color.web("#ffffff") else Color.web("#656565");


	public var node : Shape;

	init { 
		node.stroke = Color.WHITE;
	}

	public override function create(): Node {
	   return Group {
			content: [
				Stack { content: [
				    	node,
						//ImageView { image: localImage x: 0 y: 0 scaleX : .15 scaleY : .15 }
						Text{ x: 0, y: 0, 
						  content: localBean.getName() 
						  fill: bind hoverColor
						  font : Font.font("Verdana",8);
					}

					]
					translateX: node.boundsInLocal.minX
					translateY: node.boundsInLocal.minY
				}
			] // content
		}; // group
	}
	
	//jaccus: I can't find a way to bind localColor variable to node.fill attribute so I refresh it manually
	public function refreshColor() {
	    node.fill = localColor;
	}
    public function getName() : String{
        return localBean.getName();
    }
	public function showDetails (s:Scene,ev:MouseEvent) : Window {
		var tresc : Group = Group {
			content : [

                Group {

                content: [ Flow {

                    width: 500,

                    translateX: 50,
                    translateY: 50,

                content: [

                Text{ x: 0, y: 0,

                    content: localBean.getName();
                    font: Font { size: 40 }

                    translateY: 0

                    }

			Text{ x: 0, y: 0,
                    content: localBean.getDescription();
                    font: Font { size: 16, embolden: true }
                    fill: Color.web("#333333"),
                    wrappingWidth: Constants.FRAME_WIDTH-700;
                    translateY: 80

                    }
                ];

        } // Flow - end

                 ImageView {
			image: Image {
                        url: "{__DIR__}img/locals/{localBean.getImageurl()}"
                        backgroundLoading: true }
                        fitWidth: 250
                        preserveRatio: true

                        translateX: Constants.FRAME_WIDTH-500
                        translateY: 50  //TODO: scale images for smaller window
                        }
                   ];
                }
				
			]
		}
		var localWindow = Window{ tresc: Group { content: tresc },clickX:ev.x,clickY:ev.y };
	    insert localWindow into s.content;

        Main.globalCounter.playFromStart();
        Effects.fadeIn(localWindow);

        return localWindow;

	}
	
	public override function toString() : String {
	    return "local{localId.toString()}";
	}
}